package entity

import (
	"errors"
	"fmt"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//INDI_LSPH_GLXX 用户
type INDI_LSPH_GLXX struct {
	ID     int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CUID   int    `gorm:"column:CUID"`
	KHMC   string `gorm:"column:KHMC;"`
	ZJHM   string `gorm:"column:ZJHM;"`
	CCUID  int    `gorm:"column:CCUID"`
	QYMC   string `gorm:"column:QYMC;"`
	SHXYDM string `gorm:"column:SHXYDM;"`
	ZZJGM  string `gorm:"column:ZZJGM;"`
	ZW     string `gorm:"column:ZW;"`
	POXM   string `gorm:"column:POXM;"`
	POZJHM string `gorm:"column:POZJHM;"`
	EMID   int    `gorm:"column:EMID"`
	KHJL   string `gorm:"column:KHJL;"`
	BZ     string `gorm:"column:BZ"`
}

//TableName User用户表名
func (INDI_LSPH_GLXX) TableName() string {
	return "T_INDI_LSPH_GLXX"
}

//BeforeCreate 插入前的触发器
func (u *INDI_LSPH_GLXX) BeforeCreate(tx *gorm.DB) (err error) {
	var cuid int
	sqlEx := fmt.Sprintf(`select ID  from T_INDI_CLIE where IDNO = ?`)
	row := tx.Raw(sqlEx, u.ZJHM).Row()
	row.Scan(&cuid)
	if cuid == 0 {
		info := fmt.Sprintf("证件号码%s在系统中不存在，请修改！", u.ZJHM)
		return errors.New(info)
	}
	var ccuid int
	sqlEx = fmt.Sprintf(`select ID  from T_COMP_CLIE where CUNM = ? OR ENCD = ?`)
	row = tx.Raw(sqlEx, u.QYMC, u.ZZJGM).Row()
	row.Scan(&ccuid)
	if ccuid == 0 {
		info := fmt.Sprintf("关联企业%s在系统中不存在，请修改！", u.QYMC)
		return errors.New(info)
	}
	u.CUID = cuid
	u.CCUID = ccuid
	return
}

//AfterCreate 插入后的触发器
func (u *INDI_LSPH_GLXX) AfterCreate(tx *gorm.DB) (err error) {
	// tx.Exec("UPDATE T_SysUser SET EMID=0")
	// base.CacheUser(u.ID, u.Name, *u.OrganizationID)
	// sql := "select ParentOrganID from V_ParentOrgan where OrganID=?"
	// row := tx.Raw(sql, *u.OrganizationID).Row()
	// var putid int
	// row.Scan(&putid)
	// if putid == 61 {
	// 	tx.Exec("INSERT INTO t_UserRole(UserId,RoleId,IsAllowCommision,IsAllowProxy)VALUES(?,?,?,?)", u.ID, 112, 0, 0)
	// }
	return
}

func INDI_LSPH_GLXXEF() base.EntityFunc {
	return func() interface{} {
		entity := INDI_LSPH_GLXX{}
		return &entity
	}
}
func INDI_LSPH_GLXXESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []INDI_LSPH_GLXX
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("INDI_LSPH_GLXX", base.NewDefaultDao(INDI_LSPH_GLXXEF(), INDI_LSPH_GLXXESF(), nil))
}
